﻿@using Abp.Application.Services.Dto;
@using FwProject.Restaurant
@using FwProject.Authorization
@using FwProject.Restaurant.Dto
@using X.PagedList
@model IPagedList<RestaurantOutputDto>
@{
    ViewBag.Title = "餐厅管理";
}
@section styles{
    @*<link href="~/Theme/css/plugins/iCheck/custom.css" rel="stylesheet">
        <link href="~/Theme/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
        <link href="~/Theme/js/plugins/layer/skin/layer.css" rel="stylesheet" />
        <link href="~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" />
        <link href="~/Scripts/fancybox-2.1.7/source/jquery.fancybox.css" rel="stylesheet" media="screen" />
        <link href="~/Theme/css/plugins/jsTree/style.min.css" rel="stylesheet" />*@

    <link href="~/Theme/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="~/Theme/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="~/Theme/js/plugins/layer/skin/layer.css" rel="stylesheet" />
    <link href="~/Scripts/webuploader-0.1.5/webuploader.css" rel="stylesheet" />
    <link href="~/Scripts/fancybox-2.1.7/source/jquery.fancybox.css" rel="stylesheet" media="screen" />
    <link href="~/Theme/css/plugins/jsTree/style.min.css" rel="stylesheet" />
    <link href="~/plugins/layui/css/layui.css" rel="stylesheet" />

    <style type="text/css">
        .input-group[class*=col-] {
            padding: 0 15px;
        }

        /*table tbody {
            display: block;
            height: 1000px;
            overflow-y: scroll;
        }

        table thead, tbody tr {
            display: table;
            width: 100%;
            table-layout: fixed;
        }

        table thead {
            width: calc( 100% - 1em )
        }*/
    </style>
}
<div class="row wrapper wrapper-content animated fadeInRight">
    <div class="col-sm-2 @(IsGranted(PermissionNames.Factory_All)?" ":" hidden")">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>工厂</h5>
                <div class="ibox-tools"></div>
            </div>
            <div class="ibox-content" style="padding-left:0px">
                <div id="cd_tree">

                </div>
            </div>
        </div>
    </div>
    <div class="@(IsGranted(PermissionNames.Factory_All)?" col-sm-10":"col-sm-12")" style="padding-left: 0;">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>@ViewBag.Title</h5>
                <div class="ibox-tools">
                    <button type="button" class="btn btn-primary btn-xs" id="downloadExcel" onclick="downloadExcel()">下载Excel导入模板</button>
                    <button type="button" class="btn btn-primary btn-xs" id="uploadExcel"><i class="layui-icon"></i>批量导入餐厅</button>
                    <a class="btn btn-primary btn-xs" href="/Restaurant/ExportExcel">Excel导出</a>
                    <button class="btn btn-primary btn-xs" onclick="CreateRestaurant()"><i class="fa fa-plus"></i>添加餐厅</button>
                </div>
            </div>
            <div class="ibox-content">
                <div class="row m-b-sm m-t-sm">
                    <form class="form-horizontal" asp-controller="Restaurant" asp-action="Index" data-ajax="true" data-ajax-update="#data_list" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-begin="AjaxBegin" data-ajax-success="AjaxSuccessCallback">
                        <input type="hidden" id="factoryid" name="factoryid" value="" />
                        <input type="hidden" id="size" name="size" value="0" />
                        <div class="col-lg-3 col-md-3">
                            <label class="control-label col-lg-4 col-md-4">餐厅：</label>
                            <div class="input-group">
                                <input type="text" id="keyword" name="keyword" placeholder="请输入餐厅名称" class="input-sm form-control">
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3">
                            <label class="control-label col-lg-4 col-md-4">司机：</label>
                            <div class="input-group">
                                <input type="hidden" name="DriverId" id="DriverId" />
                                <input type="text" id="dropdown-driverid" class="form-control" placeholder="全部">
                                <div class="input-group-btn">
                                    <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown" style="line-height:1.455;padding:6px 12px;">
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-right" role="menu" style="z-index: 9999;"></ul>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-1 col-md-1">
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-sm btn-primary"> 查询</button>
                            </span>
                        </div>
                    </form>

                </div>
                <div id="data_list">
                    @*@{Html.RenderPartial("IndexPartial", Model);}*@
                </div>
            </div>
        </div>
    </div>
</div>
<div id="modal_ajax" class="modal inmodal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
</div>
<div id="modal_del" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content animated fadeIn">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">删除</h4>
            </div>
            <form action="" method="post" novalidate="novalidate" data-ajax="true" data-ajax-success="DeletAjaxCallback" data-ajax-failure="FailureAjaxCallback" data-ajax-method="Post" role="form">
                @Html.AntiForgeryToken()
                <div class="modal-body">
                    <p>确定要删除？</p>
                    @*<input type="hidden" name="id" value="" />*@
                </div>
                <div class="modal-footer">
                    <button type="reset" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button id="btn_del_sure" type="submit" class="btn btn-danger" data-loading-text="执行中...">确定</button>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
@section scripts{
    <script src="~/Theme/js/content.min.js?v=1.0.0"></script>
    <script src="~/Theme/js/plugins/iCheck/icheck.min.js"></script>
    <script src="~/Theme/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="~/Scripts/webuploader-0.1.5/webuploader.min.js"></script>
    <script src="~/Theme/js/plugins/suggest/bootstrap-suggest2014.js"></script>
    <script src="~/Scripts/fancybox-2.1.7/source/jquery.fancybox.js"></script>
    <script src="~/Theme/js/plugins/jsTree/jstree.min.js"></script>
    <script src="~/Scripts/jquery.treegrid.min.js"></script>
    <script src="~/plugins/layui-v2.4.5/layui/layui.js"></script>

    <script type="text/javascript">

        function downloadExcel() {
            window.location.href = "/Restaurant/DownLoadExcel";
        }

        //layer.msg("test",{time:2000});
        layui.use('upload', function () {
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#uploadExcel' //绑定元素
                , url: '/Restaurant/UploadExcel' //上传接口
                , accept: 'file'
                , exts: 'xlsx|xls'
                , data: {
                    id: function () {
                        return $('#factoryid').val();
                    }
                }
                , before: function (obj) {
                    layer.load(); //上传loading
                }
                , done: function (res) {
                    if (res.success) {
                        layer.alert("导入成功！餐厅成功：" + res.result.successCount + "条；失败：" + res.result.failureCount + "条；失败原因：" + res.result.failureMessage, {
                            skin: 'layui-layer-molv' //样式类名
                            , closeBtn: 0
                        });
                        window.location.reload();
                    } else {
                        layer.msg('导入失败！')
                    }
                }
                , error: function () {
                    //请求异常回调
                    layer.msg('接口请求异常！')
                }
            });
        });

        var loadCount = 0;
        var id = 0;

        $.getJSON("/Factory/GetFactoryTree?isType=false").done(function (data) {
            CommonAjaxCallback(data, function () {
                if (data.result.length > 0) {
                    var node = data.result[0];
                    if (node.type == "GC") {
                        layer.msg('数据加载中！', {
                            icon: 6
                        });

                    }
                }
                $("#cd_tree").on('changed.jstree', function (e, data) {
                    loadCount++;
                    if (loadCount == 1) {
                        return false;
                    }
                    id = data.instance.get_node(data.selected[0]).id;
                    var fid = data.instance.get_node(data.selected[0]).original.factoryId;

                    layer.msg('页面加载中！', {
                        icon: 6
                    });
                    $("#data_list").load("/Restaurant/Index?factoryid=" + fid + "&r=" + Math.random(), function () {
                        $('#factoryid').val(fid);
                        layer.closeAll();
                        if ($('#che').parent().hasClass("checked")) {
                            $(".td1").children().addClass("hidden");
                        }
                        $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
                    });
                }).on('loaded.jstree', function () {
                    $("#cd_tree").jstree('select_node', '' + data.result[0].id + '');
                    $("#cd_tree").jstree('open_all', data.result[0]);
                })
                    .jstree({
                        'core': {
                            'data': data.result,
                            "multiple": false,
                        },
                        "types": {
                            "GC": {
                                "icon": "fa fa-folder"
                            },
                            //"department": {
                            //    "icon": "fa fa-leaf"
                            //}
                        },
                        "plugins": ["types"]
                    });
            }, null);
        }).fail(function () {
            layer.msg('连接超时，获取工厂信息失败！', {
                icon: 2,
                time: 4000
            });
        });

        var $button;
        $(document).ready(function () {

            $.getJSON("/Users/GetSuggestData?type=司机类型").done(function (data) {
                CommonAjaxCallback(data, function () {
                    bindSuggest(data.result);
                }, null);
            }).fail(function () {
                layer.msg('连接超时，获取司机信息失败！', {
                    icon: 2,
                    time: 2000
                });
            });

            $("body").on("click", ".table>tbody>tr td.collapsed i", function (e) {
                e.preventDefault();
                if ($(this).hasClass("fa-plus-square-o")) {
                    $(this).removeClass("fa-plus-square-o").addClass("fa-minus-square-o").parent().parent().next(".tb-child").removeClass("hidden");
                    var id = $(this).data("id");
                    var tb = $(this).removeClass("fa-plus-square-o").addClass("fa-minus-square-o").parent().parent().next(".tb-child").find(".tb_tbody");
                    LoadTypeNumber(tb, id);
                }
                else {

                    $(this).removeClass("fa-minus-square-o").addClass("fa-plus-square-o").parent().parent().next(".tb-child").addClass("hidden");
                }

            });

            $('#modal_ajax').on('show.bs.modal', function (e) {
                $(this).addClass('loading');
                $button = $(e.relatedTarget);
                var url = $button.data('url');
                if (!!url) {
                    url = url + ((url.indexOf("?") > -1) ? "&r=" : "?r=") + Math.random();
                    $(this).load(url, function () {
                        $.validator.unobtrusive.parse(this); //$("#form_model_create").validate();
                        $("input[name='Member.Birthday']").datepicker({ todayBtn: "linked", keyboardNavigation: !1, forceParse: !1, calendarWeeks: !0, autoclose: !0 });
                        $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
                        $(this).removeClass('loading');
                    });
                }
            });
            $('#modal_ajax').on('hidden.bs.modal', function (e) {
                $(this).empty();
            });

            $('#modal_del,#modal_password_reset').on('show.bs.modal', function (e) {
                $button = $(e.relatedTarget);
                var url = $button.data('url');
                if (!!url)
                    url = url + ((url.indexOf("?") > -1) ? "&r=" : "?r=") + Math.random();
                $(this).find("form").attr("action", url);
            });
            $('#modal_del,#modal_password_reset').on('hidden.bs.modal', function (e) {
                $(this).find("form").attr("action", "");
            });
        });

        function bindSuggest(data) {
            if ($("#dropdown-driverid") != undefined) {
                $role = $("#dropdown-driverid").bsSuggest({
                    effectiveFields: ["name"],
                    effectiveFieldsAlias: { "name": "姓名" },
                    searchFields: ["name"],
                    clearable: true,
                    getDataMethod: "data",
                    //url: "/Users/GetSuggestData?type=司机类型&keyword=",
                    data: { value: data },
                    idField: "id",
                    keyField: "name",
                    callfun: function () {
                        $("#DriverId").val("");
                    }
                }).on('onDataRequestSuccess', function (e, result) {
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword, data) {
                    $("#DriverId").val(data.id);
                    console.log('onSetSelectValue: ', keyword, data);
                }).on('onUnsetSelectValue', function () {
                    console.log('onUnsetSelectValue');
                });
            }
        }
        //function blurEmpty() {
        //    var cityObj = $("#dropdown-driverid");
        //    var DriverId = $("#DriverId");
        //    if (cityObj.val() == "") {
        //        DriverId.attr("value", "");
        //    }
        //}

        $("#dropdown-driverid").on("input", function () {
            var v = $(this).val();
            if (v == "") {
                $("#DriverId").val("");
            }
        })

        function DeletAjaxCallback(data, successInfo) {
            CommonAjaxCallback(data, function () {
                $($button).parent().parent().remove();
                $("#modal_del").modal("hide");
            }, successInfo);
        }
        function ReloadAjaxCallbackgoback(data, successInfo) {
            CommonAjaxCallback(data, function () {
                $('#modal_ajax').modal("hide");
                var id = $($button).data("id");
                var tbody = $($button).parent().parent().siblings('table').find('.tb_tbody');
                LoadTypeNumber(tbody, id);

            }, successInfo);
        }
        function CreateRestaurant() {
            top.window.openTab(1004, '添加餐厅', 'Restaurant/Create?factoryinfoId=' + $('#factoryid').val());
        }

        function ModifyRestaurant(id) {
            top.window.openTab(1005, '编辑餐厅', 'Restaurant/Edit?id=' + id + "&factoryinfoId=" + $('#factoryid').val());
        }

        function LoadTypeNumber(tbody, id) {
            tbody.empty();
            $.getJSON("/RestaurantTypeNumber/GetTypeNumbers?id=" + id).done(function (data) {
                if (data.success) {
                    if (data.result.length > 0) {
                        for (var i = 0; i < data.result.length; i++) {
                            var tr = $('<tr>');
                            var unit = data.result[i].typeNumberInfoModel.unit;
                            if (unit == "0") {
                                unit = "个";
                            }
                            else if (unit == "1") {
                                unit = "箱";
                            }
                            else if (unit == "2") {
                                unit = "件";
                            }
                            else if (unit == "3") {
                                unit = "套";
                            }
                            tr.append('<td>' + data.result[i].typeNumberInfoModel.name + '</td>').append('<td>' + unit + '</td>').append('<td>' + data.result[i].typeNumberInfoModel.count + '</td>').append('<td>' + data.result[i].price + '</td>').append('<td>' + data.result[i].typeNumberInfoModel.readme + '</td>').append('<td><button class="btn btn-success btn-xs" data-toggle="modal" data-target="#modal_ajax" data-url="/RestaurantTypeNumber/Edit?id=' + data.result[i].id + '"><i class="fa fa-edit" ></i>编辑</button> <button class="btn btn-danger btn-xs" data-toggle="modal" data-target="#modal_del" data-url="/RestaurantTypeNumber/DeleteTypeNumber?id=' + data.result[i].id + ' "><i class="fa fa-edit"></i>删除</button></td>');
                            tbody.append(tr);
                        }
                    }
                }
            })
        }

        $("#data_list").on("change", "#pageSize", function () {
            var fid = $('#factoryid').val();
            var size = $("option:selected", this).val();

            var pageurl = $("#pageurl").val();
            $("#data_list").load("/Restaurant/Index?factoryid=" + fid + "&size=" + size + "&r=" + Math.random() + pageurl, function () {
                layer.closeAll();
                $("#size").val($("option:selected", this).val());
                if ($('#che').parent().hasClass("checked")) {
                    $(".td1").children().addClass("hidden");
                }
                $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
            });
        });
    </script>
}
